package com.example.bloodbankproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import com.example.bloodbankproject.global.GetResponse;
import com.example.bloodbankproject.global.Session;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class SplashScreen extends Activity {
	String message;

	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.splashscreen);
		UrlConnection conn = new UrlConnection();
		String url = "https://login.salesforce.com/services/oauth2/token?grant_type=password&" +
				"client_id=3MVG9Y6d_Btp4xp6X4kbP45tfmJb0zrJo_G8BrRLM4SMe_y_fCvsHKQAfT.zQHMLqR4SZgou9o3Oaqeq1I7bU&" +
				"client_secret=3939012323817525586&username=durga@bloodbank.com&password=appshark@12345oKnXnavaa1CuZLXk8rxkYDHC";
		conn.execute(url);
	}                          

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		setContentView(R.layout.splashscreen);
		super.onConfigurationChanged(newConfig);

	}   
   
	class UrlConnection extends AsyncTask<String, Void, String> {

		private HttpURLConnection http_url_conn;
		private String response;
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			try {

				URL w_Url = new URL(params[0]);
				http_url_conn = (HttpURLConnection) w_Url.openConnection();
				http_url_conn.setRequestMethod("POST");
				http_url_conn.setRequestProperty("Content-type",
						"application/json");

				Log.i("NCS",
						"Response code is  :" + http_url_conn.getResponseCode());
				Log.i("NCS",
						"Response Method is  :"
								+ http_url_conn.getRequestMethod());
				Log.i("NCS",
						"Response Message is  :"    
								+ http_url_conn.getResponseMessage());

				response =GetResponse.convert_To_String(http_url_conn.getInputStream());

				JSONObject jsonObject = new JSONObject(response);

				Session.sInstanceUrl = jsonObject.getString("instance_url");
				Session.sAccessToken = jsonObject.getString("access_token");

				Log.i("NCS", "InstanceUrl" + Session.sInstanceUrl);

				return response;

			} catch (Exception e) {
				e.printStackTrace();
			}
			return response;
		}

		@Override
		protected void onPostExecute(String result) {
			
			if(result!=null){             
				
				startActivity(new Intent(getApplicationContext(),ModulesActivity.class));
			}
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}


	}

}
